﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Command;
using XamlingCore.Infrastructure.Dialog;


namespace XamlingCore.Infrastructure.View.Implementation
{
    public class ShowDialogViewModelBase : ViewModelBase
    {
        private object headerContent;
        private object messageContent;
        private object footerContent;
        private DialogMessage dialogMessage;
        private TimeSpan dialogTimeOut;

        private Action<DialogResult> dialogComplete;

        private Action closeDialog;

        public RelayCommand CloseDialogCommand { get; set; }
        public RelayCommand OkayCommand { get; set; }
        public RelayCommand CancelCommand { get; set; }
        public RelayCommand YesCommand { get; set; }
        public RelayCommand NoCommand { get; set; }
        public RelayCommand RetryCommand { get; set; }

        private DialogResult result;


        public ShowDialogViewModelBase()
        {
            CloseDialogCommand = new RelayCommand(OnCloseDialog);
            OkayCommand = new RelayCommand(OnOkay);
            CancelCommand = new RelayCommand(OnCancel);
            YesCommand = new RelayCommand(OnYes);
            NoCommand = new RelayCommand(OnNo);
            RetryCommand = new RelayCommand(OnRetry);
        }

        private void OnRetry()
        {
            var dr = new DialogResult()
            {
                Data = this,
                DialogResultCode = DialogResultCode.Retry
            };

            Result = dr;

            FinishDialog(dr);
        }

        protected virtual void OnCloseDialog()
        {
            var dr = new DialogResult()
                         {
                             Data = this,
                             DialogResultCode = DialogResultCode.Cancel
                         };
            FinishDialog(dr);
        }

        protected virtual void OnOkay()
        {
            var dr = new DialogResult()
            {
                Data = this,
                DialogResultCode = DialogResultCode.Ok
            };

            Result = dr;

            FinishDialog(dr);
        }

        protected virtual void OnCancel()
        {
            var dr = new DialogResult()
            {
                Data = this,
                DialogResultCode = DialogResultCode.Cancel
            };

            Result = dr;

            FinishDialog(dr);
        }

        protected virtual void OnYes()
        {
            var dr = new DialogResult()
            {
                Data = this,
                DialogResultCode = DialogResultCode.Yes
            };

            Result = dr;

            FinishDialog(dr);
        }

        protected virtual void OnNo()
        {
            var dr = new DialogResult()
            {
                Data = this,
                DialogResultCode = DialogResultCode.No
            };

            Result = dr;

            FinishDialog(dr);
        }

        protected void FinishDialog(DialogResult result)
        {
            if (DialogComplete != null)
            {
                DialogComplete(result);
            }
            if (CloseDialog != null)
            {
                CloseDialog();
            }
        }

        internal Action CloseDialog
        {
            get { return closeDialog; }
            set { closeDialog = value; }
        }

        public DialogResult Result
        {
            get { return result; }
            set
            {
                result = value;
                OnPropertyChanged("Result");
            }
        }

        public object HeaderContent
        {
            get { return headerContent; }
            set
            {
                headerContent = value;
                OnPropertyChanged("HeaderContent");
            }
        }

        public object MessageContent
        {
            get { return messageContent; }
            set
            {
                messageContent = value;
                OnPropertyChanged("MessageContent");
            }
        }

        public object FooterContent
        {
            get { return footerContent; }
            set
            {
                footerContent = value;
                OnPropertyChanged("FooterContent");
            }
        }

        public DialogMessage DialogMessage
        {
            get { return dialogMessage; }
            set
            {
                dialogMessage = value;
                OnPropertyChanged("DialogMessage");
            }
        }

        public TimeSpan DialogTimeOut
        {
            get { return dialogTimeOut; }
            set
            {
                dialogTimeOut = value;
                OnPropertyChanged("DialogTimeOut");
            }
        }

        public Action<DialogResult> DialogComplete
        {
            get { return dialogComplete; }
            set { dialogComplete = value; }
        }
    }
}
